﻿using System;
using System.Collections.Generic;
using System.Linq;
using iConnector;
using System.Threading;

namespace Updater
{
    class Plugin : iPlugin
    {
        private IPluginHost pHost;
        private Thread t_BackgroundUpdater;
        private Thread t_StartUpUpdater;
        
        private static String pName;
        private static Boolean UseBackGroundUpdater;
        private static Boolean BackGroundUpdaterPaused;
        private static Boolean UseStartUpUpdater = true;
        private static Boolean AutomaticDownload;

        public Plugin()
        {
            pName = "Updater";
        }
        public String Name
        {
            get { return pName; }
            set { pName = value; }
        }
        public void Load()
        {
            Console.WriteLine(pName + ":: Hello");
            GetSettings();
            if (UseStartUpUpdater && CheckForNewVersion())
            {
                t_StartUpUpdater = new Thread(new ThreadStart(StartUpUpdater));
                t_StartUpUpdater.IsBackground = true;
                t_StartUpUpdater.Start();
                BackGroundUpdaterPaused = true;
            }
            if (!BackGroundUpdaterPaused && UseBackGroundUpdater)
            {
                t_BackgroundUpdater = new Thread(new ThreadStart(BackgroundUpdater));
                t_BackgroundUpdater.IsBackground = true;
                t_BackgroundUpdater.Start();
                //BackGroundUpdaterPaused = true;
            }
            else if (BackGroundUpdaterPaused)
            {
                Console.WriteLine(pName + ":: Background Updater is paused");
            }
        }
        public void Unload()
        {
            Console.WriteLine(pName + ":: ByeBye");
        }
        public IPluginHost Host
        {
            get { return pHost; }
            set { pHost = value; pHost.Register(this); }
        }
        /// <summary>
        /// GetFiles
        /// </summary>
        private void GetFiles()
        {
            Console.WriteLine(pName + ":: Getting File 0%");
            Thread.Sleep(1000);
            Console.WriteLine(pName + ":: Getting File 10%");
            Thread.Sleep(1000);
            Console.WriteLine(pName + ":: Getting File 50%");
            Thread.Sleep(1000);
            Console.WriteLine(pName + ":: Getting File 60%");
            Thread.Sleep(1000);
            Console.WriteLine(pName + ":: Getting File 70%");
            Thread.Sleep(1000);
            Console.WriteLine(pName + ":: Getting File 80%");
            Thread.Sleep(1000);
            Console.WriteLine(pName + ":: Getting File 90%");
            Thread.Sleep(1000);
            Console.WriteLine(pName + ":: Getting File 100%");
        }
        /// <summary>
        /// StartUpUpdater
        /// </summary>
        private void StartUpUpdater()
        {
            if (AutomaticDownload)
            {
                GetFiles();
            }
            else
            {
                Console.WriteLine(pName + ":: Update Is ready to Download But Automatic Download Is stopped");
            }
            BackGroundUpdaterPaused = false;
            UseStartUpUpdater = false;
            Load();
        }
        /// <summary>
        /// BackgroundUpdater
        /// </summary>
        private void BackgroundUpdater()
        {
            while (!BackGroundUpdaterPaused)
            {
                Console.WriteLine(pName + ":: Background Checking");
                if (CheckForNewVersion())
                {
                    Console.WriteLine(pName + ":: Background Checking:: New version Found");
                }
                Thread.Sleep(3000);
            }
        }
        /// <summary>
        /// Gets Updater Settings
        /// </summary>
        private void GetSettings()
        {
            UseBackGroundUpdater = true;
            AutomaticDownload = true;
            //UseStartUpUpdater = true;
        }
        /// <summary>
        /// Checks For new Version on Remote Server
        /// </summary>
        /// <returns></returns>
        private static Boolean CheckForNewVersion()
        {
            Console.WriteLine(pName +":: Checking For new Version");
            if (true)
            {
                //Has Update
                Console.WriteLine(pName + ":: We have update to get");
                return true;
            }
            else
            {
                // We are updated
                Console.WriteLine(pName + ":: We are Updated");
                return false;
            }
        }
    }
    class Updater
    {

    }
}
